package com.yg.controller;


import com.yg.config.result.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yg.entity.singleEntity.QualityTeacher;
import com.yg.service.QualityTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 对应sheet06，此表是质量工程教师表，对应表中的名师部分 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */

@RestController
@RequestMapping("/qualityTeacher")
@CrossOrigin
@Api(tags = "对应第六张表")
public class QualityTeacherController {

    @Autowired
    private QualityTeacherService teacherService;

    /**
     * 添加质量工程教师
     *
     * @param qualityTeacher
     * @return
     */
    @ApiOperation("添加质量工程教师")
    @PostMapping("postTeacher")
    public R postTeacher(
            @ApiParam(name = "质量工程教师数据") @RequestBody(required = true) QualityTeacher qualityTeacher
    ) {
        boolean flag = teacherService.save(qualityTeacher);

        return flag ? R.OK() : R.ERROR().message("添加失败");
    }

    /**
     * 获得所有质量工程教师信息
     *
     * @param current 当前页
     * @param limit   每页限制的条目
     * @return
     */
    @ApiOperation("获得所有质量工程教师信息")
    @GetMapping("getAllTeacher")
    public R getAllTeacher(
            @ApiParam(name = "current", value = "当前页", required = true) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = true) @RequestParam(name = "limit") long limit
    ) {
        try {
            Page<QualityTeacher> page = new Page<>(current, limit);
            QueryWrapper<QualityTeacher> teacherQueryWrapper = new QueryWrapper<>();
            teacherQueryWrapper.eq("deleted", 0);
            teacherService.page(page, teacherQueryWrapper);
            List<QualityTeacher> list = page.getRecords();
            long total = page.getTotal();
            long pages = page.getPages();

            return R.OK().data("teacherList", list).data("total", total).data("pages", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询失败");
        }
    }

    /**
     * 条件查询质量工程教师数据
     *
     * @param current        当前页
     * @param limit          每页限制的条目
     * @param qualityTeacher 教师查询条件
     * @return
     */
    @ApiOperation("条件查询质量工程教师数据")
    @PostMapping("selectTeacherByCondition")
    public R selectTeacherByCondition(
            @ApiParam(name = "current", value = "当前页", required = false) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = false) @RequestParam(name = "limit") long limit,
            @ApiParam(name = "qualityTeacher", value = "质量工程教师表", required = false) @RequestBody(required = false) QualityTeacher qualityTeacher
    ) {
        try {
            Page<QualityTeacher> page = new Page<>(current, limit);

            QueryWrapper<QualityTeacher> wrapper = new QueryWrapper<>();
            wrapper.eq("deleted", 0);
            if (!StringUtils.isEmpty(qualityTeacher.getName())) {
                wrapper.like("name", qualityTeacher.getName());
            }


            teacherService.page(page, wrapper);
            List<QualityTeacher> list = page.getRecords();
            long total = page.getTotal();
            long pages = page.getPages();

            return R.OK().data("teacherList", list).data("total", total).data("pages", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询失败");
        }

    }

    /**
     * 质量工程教师修改
     *
     * @param qualityTeacher
     * @return
     */
    @ApiOperation("质量工程教师修改")
    @PutMapping("putTeacher")
    public R putTeacher(
            @ApiParam(name = "qualityTeacher", value = "修改后的教师数据", required = false) @RequestBody QualityTeacher qualityTeacher
    ) {
        QueryWrapper<QualityTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("id", qualityTeacher.getId());
        boolean flag = teacherService.update(qualityTeacher, wrapper);
        return flag ? R.OK() : R.ERROR().message("修改失败");
    }

    /**
     * 删除质量工程教师信息
     *
     * @param qualityTeacher
     * @return
     */
    @ApiOperation("删除质量工程教师信息")
    @DeleteMapping("deleteTeacher")
    public R deleteTeacher(
            @ApiParam(name = "qualityTeacher", value = "教师数据", required = false) @RequestBody QualityTeacher qualityTeacher
    ) {

        String id = null;
        try {
            id = qualityTeacher.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询信息错误");
        }

        try {
            boolean flag = teacherService.removeById(id);
            return flag ? R.OK() : R.ERROR();
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("删除失败");
        }
    }
}

